import { DailyDetailsItemInfo } from "../Data/DailyDetailsItemInfo";
import { EventNames } from "../Data/EventNames";
import { DailyDetailsItemBase } from "./DailyDetailsItem.generated";

const { regClass, property } = Laya;

@regClass()
export class DailyDetailsItem extends DailyDetailsItemBase {
    /**日报详情信息 */
    private dailyDetailsItemInfo: DailyDetailsItemInfo = null;
    onAwake(): void {
        this.addEventListener();
        this.visible = false;
    }

    onDestroy(): void {
        this.removeEventListener();
    }

    public showView(info: DailyDetailsItemInfo): void {
        this.visible = true;
        this.dailyDetailsItemInfo = info;
        this.labelDailyDetailsTitle.text = info.dailyTitle;
        this.labelDailyDetailsDate.text = info.dailyDate;
        if (info.dailyContent.length != 0) {
            this.generateDailyContent(info.dailyContent);
        }
        this.generateDailyPicture();
    }

    private addEventListener(): void {
        this.btnClose.on(Laya.Event.CLICK, this, this.onClickCloseBtn);
        this.imgBigDailyPictureParent.on(Laya.Event.CLICK, this, this.hideBigDailyPicture);
    }

    private removeEventListener(): void {
        this.btnClose.off(Laya.Event.CLICK, this, this.onClickCloseBtn);
        this.imgBigDailyPictureParent.off(Laya.Event.CLICK, this, this.hideBigDailyPicture);
    }

    private onClickCloseBtn(): void {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        Laya.stage.event(EventNames.DAILY_DETAILS_HIDE, this.dailyDetailsItemInfo.dailyId);
        this.dailyDetailsVBox.destroyChildren();
        this.dailyDetailsVBox.height = 0;
        this.visible = false;
    }
    /**生成日报内容 */
    private generateDailyContent(dailyContent: string): void {
        let label: Laya.Label = new Laya.Label(dailyContent);
        this.dailyDetailsVBox.addChild(label);
        label.maxWidth = this.dailyDetailsVBox.width;
        label.fontSize = 34;
        label.fitContent = "height";
        label.color = "#000000";
        label.wordWrap = true;
        label.align = "left";
        label.valign = "middle";
        label.leading = 2;
    }
    /**生成日报图片 */
    private generateDailyPicture(): void {
        Laya.loader.load(this.dailyDetailsItemInfo.arrDailyPicturePath).then((data) => {
            for (let index = 0; index < data.length; index++) {
                let img = new Laya.Image();
                img.source = data[index];
                this.dailyDetailsVBox.addChild(img);
                img.useSourceSize = true;
                img.on(Laya.Event.CLICK, this, () => {
                    Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
                    this.showDailyBigPicture(img);
                });
                this.adaptiveSmallPicture(img);
            }
            Laya.timer.frameOnce(1, this, () => {
                let height: number = this.dailyDetailsVBox.getBounds().height;
                this.dailyDetailsVBox.height = height;
            });
        });
    }
    /**显示日报大图 */
    private showDailyBigPicture(img: Laya.Image): void {
        this.imgBigDailyPictureParent.visible = true;
        this.imgBigDailyPicture.skin = undefined;
        this.imgBigDailyPicture.source = img.source;
        this.adaptiveBigPicture(img.source);
    }
    /**隐藏日报大图 */
    private hideBigDailyPicture(): void {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.imgBigDailyPictureParent.visible = false;
        this.imgBigDailyPicture.source = null;
    }
    /**大图做适配宽高 */
    private adaptiveBigPicture(source: Laya.Texture): void {
        let sourceWidth = source.sourceWidth;
        let sourceHeight = source.sourceHeight;
        let rate: number = sourceWidth / sourceHeight;
        let rate1: number = this.width / this.height;
        if (rate >= rate1) {
            this.imgBigDailyPicture.width = this.width;
            this.imgBigDailyPicture.height = this.width / rate;
        } else {
            this.imgBigDailyPicture.height = this.height;
            this.imgBigDailyPicture.width = this.height * rate;
        }
    }
    /**小图做适配宽高 */
    private adaptiveSmallPicture(img: Laya.Image): void {
        let maxHeight: number = 500;
        let maxWidth: number = 500;
        let rate: number = img.source.sourceWidth / img.source.sourceHeight;
        if (rate >= 1) {
            img.width = maxWidth;
            img.height = maxWidth / rate;
        } else {
            img.height = maxHeight;
            img.width = maxHeight * rate;
        }
    }
}